package app.server.project.core.globaldao;

import app.server.project.core.syndb.SynDBMgr;
import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.annotation.TableField;
import com.baomidou.mybatisplus.annotation.TableId;
import com.baomidou.mybatisplus.annotation.TableName;
import jakarta.persistence.Column;
import jakarta.persistence.Entity;
import jakarta.persistence.Id;
import jakarta.persistence.Transient;
import lombok.Data;
import lombok.experimental.Accessors;
import org.hibernate.annotations.Comment;

@Data
@Entity(name = "global")
@TableName("global")
@Comment(value = "全局表")
@Accessors(chain = true)
public class GlobalEntity {


    @Id
    @Column
    @TableId
    @Comment("主键")
    private String id;

    @Column
    @TableField
    @Comment("json值")
    private String val;

    public void setVal(String val) {
        this.val = val;
        SynDBMgr.addChangeData(GlobalEntity::getVal,this);
    }


    @Transient
    @TableField(exist = false)
    private boolean synDb = false;

    public long getLongVal(){
        return Long.parseLong(this.val);
    }

    public int getIntVal(){
        return Integer.parseInt(this.val);
    }

    public <T> T getJsonVal(Class type){
        return (T) JSONUtil.toBean(this.val,type);
    }

    public static GlobalEntity of(String id){
        return new GlobalEntity()
                .setSynDb(true)
                .setId(id);
    }
}
